package com.mars.foodserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.foodserver.entity.FoodComment;
import com.mars.foodserver.entity.FoodNotes;
import com.mars.foodserver.entity.User;
import com.mars.foodserver.mapper.FoodCommentMapper;
import com.mars.foodserver.response.base.BasePageRequest;
import com.mars.foodserver.response.base.PageInfo;
import com.mars.foodserver.response.vo.FoodCommentVo;
import com.mars.foodserver.service.IFoodCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.foodserver.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-07-21
 */
@Service
public class FoodCommentServiceImpl extends ServiceImpl<FoodCommentMapper, FoodComment> implements IFoodCommentService {


    @Resource
    private IUserService userService;

    @Override
    public void createComment(Integer userId, Integer foodId, String comment) {
        FoodComment foodComment = new FoodComment();
        foodComment.setUserId(userId);
        foodComment.setFoodId(foodId);
        foodComment.setComment(comment);
        save(foodComment);
    }

    @Override
    public PageInfo<FoodCommentVo> page(BasePageRequest request) {

        IPage<FoodComment> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<FoodComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FoodComment::getCreateTime);
        List<FoodComment> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            List<FoodCommentVo> list = records.stream().map(x -> {
                FoodCommentVo foodCommentVo = new FoodCommentVo();
                foodCommentVo.setId(x.getId());
                foodCommentVo.setContent(x.getComment());
                foodCommentVo.setCreateTime(x.getCreateTime());
                User user = userService.selectById(x.getUserId());
                if (Objects.nonNull(user)) {
                    foodCommentVo.setAvatar(user.getAvatar());
                    foodCommentVo.setNickname(user.getNickname());
                }
                return foodCommentVo;
            }).collect(Collectors.toList());
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }

    @Override
    public List<FoodCommentVo> list(Integer foodId) {
        List<FoodComment> foodComments = baseMapper.selectList(new LambdaQueryWrapper<FoodComment>().
                orderByDesc(FoodComment::getCreateTime).
                eq(FoodComment::getFoodId, foodId));
        if (!CollectionUtils.isEmpty(foodComments)) {
            return foodComments.stream().map(x -> {
                FoodCommentVo vo = new FoodCommentVo();
                vo.setContent(x.getComment());
                vo.setId(x.getId());
                User user = userService.selectById(x.getUserId());
                if (Objects.nonNull(user)) {
                    vo.setAvatar(user.getAvatar());
                    vo.setNickname(user.getNickname());
                }
                return vo;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
